package org.dea.mapping;


import java.beans.BeanInfo;
import java.beans.Beans;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.beans.SimpleBeanInfo;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

import com.sun.org.apache.xerces.internal.dom.NodeImpl;

public class EasyAcceptMappingWriter {

	
	public static void writeMappingFile( Map<String,DistributedComponent> components, String file ) throws IOException, IntrospectionException{
		String location = null;
		new File(file).delete();
		Document mappingXmlDocument = DocumentUtils.createDocument(location, false);
		PrintStream ps = new PrintStream(new File(file));
		ps.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		ps.println("<EasyAccept xmlns=\"http://easyaccept.org/2006/mapping_schema.xsd\">");
		Set<String> keys = components.keySet();

		for (String string : keys) {
			ps.println("\t<DistributedComponent>");

			ps.println("\t\t<host_label>");
			ps.println("\t\t"+components.get(string).getHostLabel());
			ps.println("\t\t</host_label>");
				
			ps.println("\t\t<facade>");
			ps.println("\t\t"+components.get(string).getFacade());
			ps.println("\t\t</facade>");
			
			ps.println("\t\t<jar>");
			ps.println("\t\t"+components.get(string).getJarPath());
			ps.println("\t\t</jar>");
			
			ps.println("\t\t<host_address>");
			ps.println("\t\t"+components.get(string).getHostAddress() );
			ps.println("\t\t</host_address>");
			
			ps.println("\t\t<host_port>");
			ps.println("\t\t"+components.get(string).getHostPort());
			ps.println("\t\t</host_port>");
			
			ps.println("\t\t<args>");
			ps.println("\t\t"+components.get(string).getArgs());
			ps.println("\t\t</args>");
			
			ps.println("\t</DistributedComponent>");
		}
		ps.println("</EasyAccept>");
		ps.close();
	}
	
	public static void main(String[] args) throws IOException, IntrospectionException {
		Map<String,DistributedComponent> components = EasyAcceptMappingReader.processMappingFile("test/xml/mapping.xml");
		EasyAcceptMappingWriter.writeMappingFile(components, "test/xml/mappingClone.xml");
	}

}
